package cn.bdqn.controller.bill;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.bdqn.service.bill.BillService;
import cn.bdqn.pojo.Bill;


@Controller
@RequestMapping("/bill")
public class BillController {
	@Resource
	private BillService billService;

	private Bill bill=null;
	
	@RequestMapping("/billlist")
	public String selectBill(Model model, HttpServletRequest rq) {
		String pageIndex = rq.getParameter("pageIndex");
		String rec_state=rq.getParameter("rec_state");
		System.out.println("rec_state>>>>"+rec_state);
		if (rec_state == null) {
			rec_state = "0";
		}
		if (pageIndex == null) {
			pageIndex = "1";
		}
		int pageCurNO = Integer.parseInt(pageIndex);
		List<Bill> list = billService.getBillList(Integer.parseInt(rec_state),
				(pageCurNO - 1) * 5, 5);
		int getcount = billService.getCount(Integer.parseInt(rec_state));
		System.out.println("getcount>>>>>>>>>>>>>"+getcount);
		int count = getcount % 5 == 0 ? getcount / 5 : getcount / 5 + 1;
		System.out.println("pageIndex:"+pageIndex);
		System.out.println("getcount:"+getcount);
		System.out.println("count:"+count);
		model.addAttribute("listBill", list);
		model.addAttribute("pageIndex", pageIndex);
		model.addAttribute("count", count);
		return "billlist";
	}
	
	@RequestMapping("/addBill")
	public String getadd(HttpServletRequest rq, Model mode) {
		return "billadd";
	}
	
	@RequestMapping("/addBilltj")
	public String add(HttpServletRequest rq,Model model){
		String rec_name=rq.getParameter("rec_name");
		String rec_phone=rq.getParameter("rec_phone");
		String rec_identity=rq.getParameter("rec_identity");
		String rec_department=rq.getParameter("rec_department");
		String rec_jobs=rq.getParameter("rec_jobs");
		String rec_address=rq.getParameter("rec_address");
		String rec_state=rq.getParameter("rec_state");
		String rec_source=rq.getParameter("rec_source");
		
		Bill bill=new Bill();
		
		bill.setRec_name(rec_name);
		bill.setRec_phone(rec_phone);
		bill.setRec_identity(rec_identity);
		bill.setRec_department(Integer.parseInt(rec_department));
		bill.setRec_jobs(Integer.parseInt(rec_jobs));
		bill.setRec_address(rec_address);
		bill.setRec_state(Integer.parseInt(rec_state));
		bill.setRec_source(rec_source);
		int i=billService.add(bill);
		if(i>0){
			return "redirect:/bill/billlist";
		}else{
			return "billadd";
		}
	}
	
	@RequestMapping("/deleteBill")
	public String getDelete(String rec_id) {
		int i = billService.deleteBill(Integer.parseInt(rec_id));
		if (i > 0) {
			return "redirect:/bill/billlist";
		} else {
			return "bill";
		}
	}
	
	@RequestMapping("/modifyBill")
	public String getupdate(HttpServletRequest rq, Model mode) {
		String rec_id = rq.getParameter("rec_id");
		bill = billService.selectId(Integer.parseInt(rec_id));
		mode.addAttribute("bill", bill);
		return "billmodify";
	}
	
	@RequestMapping("/updateBillxg")
	public String update(HttpServletRequest rq,Model model){
		String rec_id = rq.getParameter("rec_id");
		String rec_name=rq.getParameter("rec_name");
		String rec_phone=rq.getParameter("rec_phone");
		String rec_identity=rq.getParameter("rec_identity");
		String rec_department=rq.getParameter("rec_department");
		String rec_jobs=rq.getParameter("rec_jobs");
		String rec_address=rq.getParameter("rec_address");
		String rec_state=rq.getParameter("rec_state");
		String rec_source=rq.getParameter("rec_source");
		
		Bill bill=new Bill();
		
		bill.setRec_id(Integer.parseInt(rec_id));
		bill.setRec_name(rec_name);
		bill.setRec_phone(rec_phone);
		bill.setRec_identity(rec_identity);
		bill.setRec_department(Integer.parseInt(rec_department));
		bill.setRec_jobs(Integer.parseInt(rec_jobs));
		bill.setRec_address(rec_address);
		bill.setRec_state(Integer.parseInt(rec_state));
		bill.setRec_source(rec_source);
		
		System.out.println(bill.toString());
		int i = billService.update(bill);
		if (i > 0) {
			return "redirect:/bill/billlist";
		} else {
			return "billmodify";
		}
	}
	
	@RequestMapping("/viewBill")
	public String getviewBill(HttpServletRequest rq, Model mode) {
		String rec_id = rq.getParameter("rec_id");
		bill = billService.selectId(Integer.parseInt(rec_id));
		mode.addAttribute("bill", bill);
		System.out.println(bill.toString());
		return "billview";
	}
	
}
